package com.allen.admin.service.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.allen.admin.service.common.domain.AdminRole;
import com.allen.admin.service.common.domain.AdminUser;
import com.allen.admin.service.common.service.AdminUserService;
import com.allen.admin.service.server.manager.impl.AdminRoleManager;
import com.allen.admin.service.server.manager.impl.AdminUserManager;
import com.xxx.config.ModelResult;
import com.xxx.page.DataPage;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {

	private Map<String, Integer> pollIndexMap = new HashMap<>();

	@SuppressWarnings("unused")
	private final static Logger logger = LoggerFactory.getLogger(AdminUserServiceImpl.class);
	
	@Autowired
	private AdminUserManager adminUserManager;
	
	@Autowired
	private AdminRoleManager adminRoleManager;
	
	@Override
	public ModelResult<AdminUser> create(AdminUser obj) {
		ModelResult<AdminUser> resultModelResult = new ModelResult<>();
		AdminUser user = adminUserManager.insert(obj);
		resultModelResult.withModel(user);
		return resultModelResult;
	}

	@Override
	public ModelResult<AdminUser> update(AdminUser obj) {
		ModelResult<AdminUser> resultModelResult = new ModelResult<>();
		AdminUser user = adminUserManager.update(obj);
		resultModelResult.withModel(user);
		return resultModelResult;
	}

	@Override
	public ModelResult<AdminUser> query(Long id) {
		ModelResult<AdminUser> resultModelResult = new ModelResult<>();
		AdminUser user = adminUserManager.select(id);
		resultModelResult.withModel(user);
		return resultModelResult;
	}

	@Override
	public ModelResult<AdminUser> query(String account) {
		ModelResult<AdminUser> resultModelResult = new ModelResult<>();
		AdminUser user = adminUserManager.select(account);
		resultModelResult.withModel(user);
		return resultModelResult;
	}

	@Override
	public ModelResult<AdminUser> query(String account, String password) {
		ModelResult<AdminUser> resultModelResult = new ModelResult<>();
		AdminUser user = adminUserManager.select(account, password);
		resultModelResult.withModel(user);
		return resultModelResult;
	}
	
	@Override
	public ModelResult<List<AdminUser>> query(List<Long> ids) {
		ModelResult<List<AdminUser>> resultModelResult = new ModelResult<>();
		List<AdminUser> users = adminUserManager.select(ids);
		resultModelResult.withModel(users);
		return resultModelResult;
	}
	
	@Override
	public ModelResult<List<AdminUser>> queryByDepartment(Integer departmentId, Integer status) {
		ModelResult<List<AdminUser>> resultModelResult = new ModelResult<>();
		List<AdminUser> users = adminUserManager.selectByDepartment(departmentId, status);
		resultModelResult.withModel(users);
		return resultModelResult;
	}
	
	@Override
	public ModelResult<DataPage<AdminUser>> queryPage(DataPage<AdminUser> dataPage, AdminUser qObj) {
		ModelResult<DataPage<AdminUser>> resultModelResult = new ModelResult<>();
		dataPage = adminUserManager.queryPage(dataPage, qObj);
		resultModelResult.withModel(dataPage);
		return resultModelResult;
	}

	@Override
	public ModelResult<List<AdminUser>> queryByRoleCode(String roleCode) {
		ModelResult<List<AdminUser>> resultModelResult = new ModelResult<>();
		AdminRole role = adminRoleManager.selectByCode(roleCode);
		resultModelResult.withModel(adminUserManager.selectByRoleId(role.getId()));
		return resultModelResult;
	}

	@Override
	public ModelResult<AdminUser> pollUserByRoleCode(String roleCode) {
		ModelResult<AdminUser> resultModelResult = new ModelResult<>();

		ModelResult<List<AdminUser>> usersModelResult = queryByRoleCode(roleCode);
		List<AdminUser> users = usersModelResult.getModel();
		if(CollectionUtils.isEmpty(users)) {
			return resultModelResult;
		}
		Integer index = pollIndexMap.get(roleCode);
		if(index == null) {
			index = 0;
		} else if(index >= users.size()) {
			index = 0;
		}
		pollIndexMap.put(roleCode, index + 1);
		return resultModelResult.withModel(users.get(index));
	}

	@Override
	public ModelResult<List<AdminUser>> queryByAccounts(List<String> accounts) {
		ModelResult<List<AdminUser>> resultModelResult = new ModelResult<>();
		List<AdminUser> users = adminUserManager.queryByAccounts(accounts);
		resultModelResult.withModel(users);
		return resultModelResult;
	}
}
